#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from PageFaultSequence import PageFaultSequence
from PageFaultSequence import PageFaultResolutionType
from gpgpu.ModifierUtils import AccessFaultModifier


# This test verifies recovery from a Access Exception on a branch operation.
class MainSequence(PageFaultSequence):
    def __init__(self, gen_thread, name=None):
        super().__init__(gen_thread, name)
        self._mInstrList = ("JAL##CALM", "JALR##CALM")
        self._mExceptionCodes = 1
        self._mExceptionSubCodes = {}  # no sub-codes for greencode

    # Create an instance of the appropriate page fault modifier.
    def createPageFaultModifier(self):
        return AccessFaultModifier(self.genThread)

    # Create a list of instructions to choose from to trigger an
    # access exception..
    def getInstructionList(self):
        return self._mInstrList

    # Return exception sub-codes.
    def getExceptionSubCodes(self):
        return self._mExceptionSubCodes

    # Return the expected resolution type for the page fault.
    #
    #  @param aFaultLevels  - the translation level at which the page
    #                           fault was triggered.
    #  @param aFastHandlers - indicates whether or not the fast handlers
    #                           are enabled.
    def getPageFaultResolutionType(self, aFaultLevels, aFastHandlers):
        resolution_type = PageFaultResolutionType.SKIP_INSTRUCTION
        if any([x in aFaultLevels for x in (0, 1, 2, 3)]) and (not FastHandlers):
            resolution_type = PageFaultResolutionType.RE_EXECUTE_INSTRUCTION
        return resolution_type


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
